﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetDxComponents.Design.Editors;
using ProgNetComponents;
using System.Drawing.Design;
using ProgNetDxComponents.ExtenderProviders.Browsers;
using ProgNetComponents.Data;

namespace ProgNetAppBlocks.Forms
{
    public partial class ProgNetRecordForm : ProgNetBaseDbForm
    {
        #region fields
        protected Browser Browser
        {
            get
            {
                return BrowserCache.GetBrowser(BrowserName, false);
            }
        }
        protected virtual string ValueColumn
        {
            get { return Browser.ValueMember; }
        }
        protected virtual string DisplayColumn
        {
            get { return Browser.DisplayMember; }
        }
        protected virtual DataRow CurrentRow
        {
            get { return DataSource.Tables[SelectData.SingleRecordQueries[0].TableDataSetName].Rows[0]; }
        }
        #endregion

        #region properties
        [Browsable(false)]
        public ProgNetSpreadSheetForm SpreadSheetForm
        {
            get;
            set;
        }

        [Browsable(false)]
        public bool WasModified
        {
            get;
            private set;
        }

        [Category(Categories.ProgNetSettings),
        Editor(typeof(BrowserNameEditor), typeof(UITypeEditor))]
        public string BrowserName
        {
            get;
            set;
        }

        [Category(Categories.ProgNetSettings)]
        public string NewRecordFormText
        {
            get;
            set;
        }
        #endregion

        public ProgNetRecordForm()
        {
            InitializeComponent();
        }
        public ProgNetRecordForm(object record)
        {
            InitializeComponent();
            CurrentRecordId = record;
        }

        protected override bool Save()
        {
            CurrentRow.EndEdit();
            bool result = base.Save();
            if (!result)
                CurrentRow.BeginEdit();
            else
                WasModified = true;
            return result;
        }
        protected virtual ObjectKeyValuePair[] GetOpenParameters()
        {
            return new ObjectKeyValuePair[] { new ObjectKeyValuePair("@" + ValueColumn, CurrentRecordId) };
        }

        protected virtual bool OpenRecord(object record)
        {
            bool result = false;
            CurrentRecordId = record;
            if (CurrentRecordId.IsNotNull())
            {
                result = Manager.FillDataSet(DataSource, GetOpenParameters());
                if (!result)
                    if (!Manager.GetAutoLogExceptions)
                        Manager.LastException.Log();
            }
            else
            {
                result = true;
                NewRecord();
            }
            this.Text = FormText;
            return result;
        }

        protected virtual void NewRecord()
        {
            DataSource.Clear();
            DataSource.AcceptChanges();
            DataRow r = DataSource.Tables[SelectData.SingleRecordQueries[0].TableDataSetName].NewRow();
            DataSource.Tables[SelectData.SingleRecordQueries[0].TableDataSetName].Rows.Add(r);
            r.BeginEdit();
        }

        protected override bool OpenRecord()
        {
            bool result = false;
            ObjectDialogResult dlg = Browser.ShowDialog();
            if (dlg.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                result = OpenRecord(dlg.Value);
            }
            return result;
        }

        protected override bool Delete()
        {
            CurrentRow.DeleteRowWithRelation();
            return Save();
        }

        protected override bool OpenEntry()
        {
            return OpenRecord(CurrentRecordId);
        }

        protected override string FormText
        {
            get
            {
                string text = base.FormText;
                if (modeProvider1.CurrentMode == ProgNetDxComponents.ExtenderProviders.FormMode.New)
                    text += " [" + NewRecordFormText + "]";
                else
                {
                    text += " [" + CurrentRow[DisplayColumn].AsString() + "]";
                }
                return text;
            }
        }

        protected override bool IsSingleQuery
        {
            get { return true; }
        }

    }
}
